/**
 * 
 */
package com.desitek.cricket.dao.hibernate;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.desitek.cricket.entity.Groups;
import com.desitek.cricket.entity.User;
import com.desitek.cricket.repository.UserRepository;

/**
 * @author Dhiraj
 * 
 */
public class UserDAO extends GenericDAO<User> implements UserRepository {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desitek.cricket.repository.UserRepository#findByUserName(java.lang.String)
	 */
	public List<User> findByUserName(String username) {

		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desitek.cricket.repository.UserRepository#getAllUsers(int, int)
	 */
	@SuppressWarnings("unchecked")
	public List<User> getAllUsers(int start, int count) {
		return getSession().createCriteria(User.class).setFirstResult(start)
				.setMaxResults(count).setComment("userDAO.getAllUsers").list();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desitek.cricket.repository.UserRepository#getByEmail(java.lang.String)
	 */
	public User getByEmail(String email) {
		return (User) getSession().createCriteria(User.class).add(
				Restrictions.eq("email", email)).uniqueResult();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desitek.cricket.repository.UserRepository#getByUsername(java.lang.String)
	 */
	public User getByUsername(String username) {
		// TODO Auto-generated method stub
		return (User) getSession().createCriteria(User.class).add(
				Restrictions.eq("username", username)).uniqueResult();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desitek.cricket.repository.UserRepository#getLastRegisteredUser()
	 */
	public User getLastRegisteredUser() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desitek.cricket.repository.UserRepository#getTotalPosts(com.desitek.cricket.entity.User)
	 */
	public int getTotalPosts(User user) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desitek.cricket.repository.UserRepository#getTotalUnreadPrivateMessages(com.desitek.cricket.entity.User)
	 */
	public int getTotalUnreadPrivateMessages(User user) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desitek.cricket.repository.UserRepository#getTotalUsers()
	 */
	public int getTotalUsers() {
		// TODO Auto-generated method stub
		return (Integer) getSession().createCriteria(User.class)
		.setProjection(Projections.rowCount())
		.setCacheable(true).setCacheRegion("userDAO.getTotalUsers")
		.setComment("userDAO.getTotalUsers")
		.uniqueResult();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desitek.cricket.repository.UserRepository#getUsersById(int)
	 */
	public User getUsersById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desitek.cricket.repository.UserRepository#isUsernameAvailable(java.lang.String,
	 *      java.lang.String)
	 */
	public boolean isUsernameAvailable(String username, String email) {
		// TODO Auto-generated method stub
		return (Integer) getSession().createCriteria(User.class).setProjection(
				Projections.rowCount()).add(
				Restrictions.eq("username", username)).add(
				Restrictions.eq("email", email)).uniqueResult() == 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desitek.cricket.repository.UserRepository#validateLogin(java.lang.String,
	 *      java.lang.String)
	 */
	public User validateLogin(String username, String password) {
		return (User) getSession().createCriteria(User.class).add(
				Restrictions.eq("username", username)).add(
				Restrictions.eq("password", password)).setComment(
				"userDAO.validateLogin").uniqueResult();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desitek.cricket.repository.UserRepository#validateLostPasswordHash(java.lang.String,
	 *      java.lang.String)
	 */
	public User validateLostPasswordHash(String username, String hash) {
		// TODO Auto-generated method stub
		return null; // (User)
						// getSession().createCriteria(User.class).add(Restrictions.eq("username",username)).add(Restrictions.eq(propertyName,
						// value));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desitek.cricket.repository.GenericRepository#delete(java.lang.Object)
	 */

}
